package com.jinzecloud.reportapproval.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinzecloud.reportapproval.bean.HandoverChecklist;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import com.jinzecloud.reportapproval.service.HandoverChecklistService;
import com.jinzecloud.reportapproval.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;


/**
 * 技术文件、资料移交清单
 *
 * @author æ­å°
 * @email 1971240051@qq.com
 * @date 2024-12-03 16:18:45
 */
@RestController
@RequestMapping("/handoverChecklist")
@RequiredArgsConstructor
public class HandoverChecklistController {

    private final HandoverChecklistService handoverChecklistService;
    private final AdminService adminService;

    @PostMapping
    public R save(@RequestBody HandoverChecklist handoverChecklist) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        handoverChecklist.setState(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        handoverChecklist.setCreatedUserId(adminService.getIdByName(authentication.getName()));
        R r = handoverChecklistService.saveHandoverChecklist(handoverChecklist);
        return r;
    }

    @PutMapping
    public R update(@RequestBody HandoverChecklist handoverChecklist) {
        handoverChecklist.setState(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        handoverChecklistService.updateById(handoverChecklist);
        return R.ok();
    }

    @PostMapping("/review")
    public R review(@RequestBody HandoverChecklist handoverChecklist) {
        handoverChecklistService.updateById(new HandoverChecklist().setId(handoverChecklist.getId()).setState(StateEnum.SAMPLE.getState()));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return handoverChecklistService.audit(auditParam);
    }

    @PostMapping("/list")
    public R list(@RequestBody DataStagingParam param) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LambdaQueryWrapper<HandoverChecklist> handoverChecklistLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (param.getSearchKey() == null){
            handoverChecklistLambdaQueryWrapper.like(HandoverChecklist::getProjectCode, param.getSearchKey()).and(w -> w.in(HandoverChecklist::getState, StateEnum.UN_SUBMIT.getState(), StateEnum.APPROVAL_PASS.getState()).eq(HandoverChecklist::getCreatedUserId, adminService.getIdByName(authentication.getName())
            ).or().eq(HandoverChecklist::getState, StateEnum.APPROVAL_PASS.getState()))
                    .and(w -> w.like( HandoverChecklist::getProjectNumber, param.getSearchKey())
                            .or().like( HandoverChecklist::getProductName, param.getSearchKey())
                            .or().like( HandoverChecklist::getHandoverPerson, param.getSearchKey()));
        }

//        handoverChecklistLambdaQueryWrapper.like(HandoverChecklist::getProjectCode, param.getSearchKey())
//                .and(w -> w.in(HandoverChecklist::getState, StateEnum.UN_SUBMIT.getState(), StateEnum.APPROVAL_PASS.getState()).eq(HandoverChecklist::getCreatedUserId, adminService.getIdByName(authentication.getName())
//                ).or().eq(HandoverChecklist::getState, StateEnum.APPROVAL_PASS.getState()));
////                .and(w -> w.like(param.getSearchKey() != null, HandoverChecklist::getProjectNumber, param.getSearchKey())
////                        .or().like(param.getSearchKey() != null, HandoverChecklist::getProductName, param.getSearchKey())
////                        .or().like(param.getSearchKey() != null, HandoverChecklist::getHandoverPerson, param.getSearchKey()));
        Page<HandoverChecklist> page = handoverChecklistService.page(new Page<>(param.getPageNo(), param.getPageSize()),
                handoverChecklistLambdaQueryWrapper);
        return R.ok().setData(page);
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(handoverChecklistService.auditList(param));
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok().setData(handoverChecklistService.getById(id));
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        handoverChecklistService.removeById(id);
        return R.ok();
    }

    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody Long[] ids) {
        handoverChecklistService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/getFilePath/{id}")
    public R getReportFilePath(@PathVariable("id") String id, HttpServletRequest request) throws IOException, IllegalAccessException {
        String path = handoverChecklistService.getFilePath(id, request);
        return R.ok().put("path", path);
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id){
        return handoverChecklistService.viewPdf(id);
    }

    @RequestMapping("/viewWord/{id}")
    public String viewWord(@PathVariable("id") Integer id){
        return handoverChecklistService.viewWord(id);
    }
}
